libname a "D:\SAS\Dataset\CRSP\Monthly Return";


data a (keep=date cusip ticker prc PRIMEXCH ret SHROUT VWRETD);
set a.Return_1952_1979 a.Return_1980_2020 a.Return_2021_2022;
run;


libname b  "D:\SAS\Dataset\Fama French Factors";

data b (keep=dateff rf);
set b.ff5factormonthly;

data b (rename=(dateff=date));
set b;
run;

proc sort data=a; by date; 
proc sort data=b; by date;


data c; merge a b; by date;
run;

data c;
set c;
year=year(date);
month=month (date);
me=prc *shrout;
stockreturn=ret-rf;
MKTRF=VWRETD-rf;
run;


libname d "D:\SAS\Project\Persistence of MTB Ratio";

data d1 (keep=cusip fyear sgaintangible);
set d.sgaintangible;
run;

data d1 (rename=(fyear=year));
set d1;
run;


libname e  "D:\SAS\Dataset\Compustat\Compustat US";

data e1 (keep=DATADATE cusip at XRD sale COGS REVT INTPN XSGA ceq ni);
set e.Compustat1950_1979 e.Compustat1980_1989 e.Compustat1990_1995 e.Compustat1996_2000 e.Compustat2001_2005 
e.Compustat2006_2010  e.Compustat2011_2020 e.Compustat2021_2023;
run;


data e1;
set e1;
if XRD=. then xrd=0;
year=year(DATADATE);
run;

proc sort data=d1; by cusip year;
proc sort data=e1; by cusip year;


data d2;
merge d1 e1; by cusip year;
run;

proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lat=lag(at);
if first.cusip then lat=.;
if sgaintangible<0 then sgaintangible=0;
run;

data d2;
set d2;
intangible=(sgaintangible+xrd)/((lat+at)/2);
assetgrowth=(at-lat)/lat;
profitability=(REVT-COGS-INTPN -XSGA)/ceq;
roe=ni/ceq;
run;


data c1 (keep=cusip year me);
set c; where month=12; 
run;

data d2;
set d2;
cusip=substr (cusip, 1, 8);
run;

proc sort data=d2; by cusip year;
proc sort data=c1; by cusip year;

data d2;
merge d2 c1; by cusip year;
mtb=me/ceq/1000;
run;


proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lintangible=lag(intangible);
lme=lag(me);
lmtb=lag(mtb);
lassetgrowth=lag(assetgrowth);
lprofitability=lag(profitability);
if first.cusip then lintangible =.;
if first.cusip then lme=.;
if first.cusip then lmtb=.;
if first.cusip then lassetgrowth=.;
if first.cusip then lprofitability=.;
run;


proc sort data=d2; by year;

proc rank data=d2 out=d3 group=5; by year;
var lmtb;
ranks elmtb; 
run;

data d4 (keep=cusip year elmtb);
set d3;
run;


proc sort data=c; by cusip year;
proc sort data=d4; by cusip year;


data f;
merge c d4; by cusip year;
run;



libname g "D:\SAS\Dataset\Fama French Factors";

data g1 (keep=DATEFF smb hml rmw cma umd);
set g.ff5factormonthly;

data g1 (rename=(dateff=date));
set g1;
run;


data g2;
set g.sgafactor;
run;


proc sort data=f; by date;
proc sort data=g1; by date;
proc sort data=g2; by date;


data h;
merge f g1 g2; by date;
run;


proc sort data=h; by date elmtb; run;

proc means data=h ;by date elmtb;
output out= h1;
var stockreturn;
run;

data h1;
set h1;
where _STAT_= "MEAN";
run;

data h2 (keep=date year MKTRF smb hml RMW CMA UMD SGAfactor);
set h;
run;

proc sort data=h2 noduplicate; by date; run; 


proc sort data=h1; by date;
proc sort data=h2; by date;

data h3;
merge h1 h2; by date;
run;



/*Pearson correlation for Table 2 */ 

proc corr data=h3; where 1962<year <=1992; 
var MKTRF smb hml RMW CMA UMD SGAfactor;
run;


proc corr data=h3; where 1992<year <=2022; 
var MKTRF smb hml RMW CMA UMD SGAfactor;
run;




/*Results for Table 4*/

proc sort data=h3; by elmtb;

proc reg data=h3;  where 1962<year <=1992; by elmtb;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactor;
run;


proc reg data=h3;  where 1992<year <=2022; by elmtb;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactor;
run;




/*Results for Table 5*/

proc reg data=h3;  where 1962<year <=1992;  
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactor /vif;
run;

proc reg data=h3;  where 1962<year <=1992; 
model SGAfactor=MKTRF smb hml RMW CMA; /*To get the coefficients of these variables, which are used to calculate the SGAfactorResidual */
run;

data h4;
set h3;
SGAfactorResidual=SGAfactor-(0.07817*MKTRF+0.19130*SMB-0.32947*HML+0.08906*RMW+0.13287*CMA);
RUN;

proc sort data=h4; by elmtb;

proc reg data=h4;  where 1962<year <=1992; by elmtb;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactorresidual;
run;




proc reg data=h3; where 1992<year <=2022;  
model SGAfactor=MKTRF smb hml RMW CMA;/*To get the coefficients of these variables, which are used to calculate the SGAfactorResidual */
run;

data h5;
set h3;
SGAfactorResidual=SGAfactor-(0.11794*MKTRF+0.22084*SMB-0.64662*HML-0.63268*RMW-0.00400*CMA);
RUN;

proc sort data=h5; by elmtb;

proc reg data=h5;  where 1992<year <=2022;   by elmtb;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactorresidual;
run;



